#include "plccommmainwindow.h"
#include "ui_plccommmainwindow.h"
#include "modbusrtu.h"
#include <QDebug>
#include <QMessageBox>
#include <QAbstractItemView>
#include <QAbstractButton>
#include <QSerialPort>

PlcCommMainWindow::PlcCommMainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::PlcCommMainWindow)
{
    ui->setupUi(this);

//    ModbusRtu *test = new ModbusRtu();
//    port = new QSerialPort();



//    QStringList a = test->searchPort();
//    QDebug debug = qDebug();
//    debug << a;

    modbusRtu = new ModbusRtu();


    addPort();

//    connect(ui->openBtn,SIGNAL(clicked()),this,SLOT(openPort()));
//    connect(ui->sendBtn,SIGNAL(clicked()),this,SLOT(sendData()));

//    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(testProtocol()));

    modbusRtu->setDataRecived = 1;

}

PlcCommMainWindow::~PlcCommMainWindow()
{
    delete ui;
}

void PlcCommMainWindow::addPort()
{
//    ui->ccbCom->addItems(modbusRtu->searchPort());
//    ui->ccbCom->view()->setFixedWidth(200);


}

void PlcCommMainWindow::openPort()
{


//    QVariant vBaudRate = ui->cbbBaud->currentData();
//    QVariant vDataBits = ui->cbbDataBit->currentData();
//    QVariant vParity = ui->cbbVerify->currentData();
//    QVariant vStopBit = ui->cbbStopBit->currentData();
//    modbusRtu->iniSerialPort(vBaudRate,vDataBits,vParity,vStopBit);
//    connect(modbusRtu,SIGNAL(readyRead()),this,SLOT(revData()),Qt::DirectConnection);
//    QString tmp = ui->ccbCom->currentText();
//  //  QStringList list = tmp.split(" ");
//    tmp = tmp.split(" ").first();
//    modbusRtu->portOpen(tmp);

}

void PlcCommMainWindow::sendData()
{
    QByteArray tmp1 ;
    tmp1.resize(1);
    tmp1[0] = 0x01;

//    modbusRtu->setDataRecived = 0;
    modbusRtu->SetReceiveTimeOt(2000);


//    qDebug() << modbusRtu->setDataRecived;

    QByteArray tmp3 ;

//    tmp3.append(modbusRtu->ReadKeepReg(1,0,2));


    modbusRtu->SetDataFormat(ModbusRtu::DCBA);
    QByteArray senddata;

    //读取线圈状态
//    QByteArray readOutput;
//    readOutput =  modbusRtu->ReadOutputStatus(1,0,2);
//    qDebug() << readOutput;

    //读取输入状态
//    QByteArray readInput;
//    readInput = modbusRtu->ReadInputStatus(1,0,2);

    //读取输入寄存器
//    QByteArray readInputReg;
//    readInputReg = modbusRtu->ReadInputReg(1,0,2);


    //强制单线圈
  bool status =  modbusRtu->ForceCoil(1,0,true);
  qDebug() << status;
  qDebug() << "send threadId" << QThread::currentThreadId();

  //  senddata = modbusRtu->PresetMultiRes(1,0,arry);
 //   modbusRtu->SendReadDate();
  //  senddata = modbusRtu->PreSetSingleReg(1,0,15);

 //    modbusRtu->sendData(port,senddata);


   //设置寄存器值
//   bool result;
//   QVector<float> arry {1.2,1.5};
//   result =  modbusRtu->PresetMultiRes(1,0,arry);

//   qDebug() << result;


//    QByteArray tmp2;
//    tmp2 = modbusRtu->ReadKeepReg(1,0,1);

//    QString strDis;
//    QString str3 = tmp2.toHex().data();
//    str3 = str3.toUpper();
//    for(int i = 0;i<str3.length ();i+=2)//填加空格
//    {
//        QString st = str3.mid (i,2);
//        strDis += st;
//        strDis += " ";
//    }

//ui->textEdit->append(strDis);
}

void PlcCommMainWindow::revData()
{
    QByteArray tmp2;
    tmp2.resize(20);
    tmp2 = modbusRtu->readAll();



 if((char)modbusRtu->SendCommandByte[1] == 0x05)
 {
     revStatu = modbusRtu->revForceCoil(tmp2);
     qDebug() << revStatu;
     qDebug() << "revdata threadId" << QThread::currentThreadId();
 }
//    tmp2 = modbusRtu->revData(port);



//    QDebug a = qDebug();
//    a << tmp2;

//    QString strDis;
//    QString str3 = tmp2.toHex().data();
//    str3 = str3.toUpper();
//    for(int i = 0;i<str3.length ();i+=2)//填加空格
//    {
//        QString st = str3.mid (i,2);
//        strDis += st;
//        strDis += " ";
//    }


    modbusRtu->setDataRecived = 0;
//    ui->textEdit->append(strDis);
}

void PlcCommMainWindow::testProtocol()
{
    QByteArray a;
    a[0] = 1;
    a[1] = 2;
    QByteArray b;

    QVector<float> arry {1.1,1.2};

    modbusRtu->SetDataFormat(ModbusRtu::DCBA);
    float data=1.23;
    modbusRtu->PresetMultiRes(1,0,arry);

  QString strDis;
  QString str3 = b.toHex().data();
  str3 = str3.toUpper();
  for(int i = 0;i<str3.length ();i+=2)//填加空格
  {
      QString st = str3.mid (i,2);
      strDis += st;
      strDis += " ";
  }

  //ui->textEdit->append(strDis);
}

